//
//  BiCarBorrowNetEngine.m
//  TrunkIndustryManageSystem
//
//  Created by 李璐西 on 15-4-28.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiCarBorrowNetEngine.h"
#import "BiNetEngine.h"
#import "ASIFormDataRequest.h"
#import "BiCarBorrowParser.h"
#import "ASINetworkQueue.h"

@implementation BiCarBorrowNetEngine
SINGLE_INSTANCE_IMPLEMENTION(BiCarBorrowNetEngine)
/**
 *   请求借车审批列表， 下拉刷新请求更多，dict为传入参数字典
 */
- (void)requestCarBorrowCheckList:(id)delegate paraDict:(NSDictionary *)dict{
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/BorrowCarCenter/getList",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_BORROW_CHECK_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_CAR_BORROW_CHECK_LIST];
}
/**
 *   请求借车审批,dict为传入参数字典
 */
-(void)requestBorrowCarCheck:(id)delegate paraDict:(NSDictionary *)dict{
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/BorrowCarCenter/modify",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_BORROW_CHECK]];
    
    [self createPutRequest:url paramDic:dict tag:NET_GET_CAR_BORROW_CHECK];
}

/**
 *   请求还车审批列表， 下拉刷新请求更多，dict为传入参数字典
 */
- (void)requestCarReturnCheckList:(id)delegate paraDict:(NSDictionary *)dict{
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/BorrowCarCenter/getList",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_RETURN_CHECK_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_CAR_RETURN_CHECK_LIST];
}
/**
 *   请求还车审批,dict为传入参数字典
 */
-(void)requestCarReturnCheck:(id)delegate paraDict:(NSDictionary *)dict{
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/BorrowCarCenter/modify",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_RETURN_CHECK]];
    
    [self createPutRequest:url paramDic:dict tag:NET_GET_CAR_RETURN_CHECK];
}

//--------------------------------------分
- (void)requestDone:(ASIHTTPRequest *)request
{
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    int httpCode = [request responseStatusCode];
    
    if (200 > httpCode || 300 <= httpCode)
    {
        NSLog(@"http error --- tag = %d, http code = %d", tag, httpCode);
        assert(0);
        return;
    }
    
    switch (tag)
    {

        case NET_GET_CAR_BORROW_CHECK_LIST:
        {
            [self onBorrowCarCheckList:request];
        }
            break;
        case NET_GET_CAR_BORROW_CHECK:
        {
            [self onBorrowCarCheck:request];
        }
            break;
            case NET_GET_CAR_RETURN_CHECK_LIST:
        {
            [self onCarReturnCheckList:request];
        }
            break;
            case NET_GET_CAR_RETURN_CHECK:
        {
            [self onCarReturnCheck:request];
        }
            break;
        default:
            break;
    }
    
}
//借车
- (void)onBorrowCarCheckList:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary  * dict = [BiCarBorrowParser parserBorrowCarCheckListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}

-(void)onBorrowCarCheck:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary  * dict = [BiCarBorrowParser parserBorrowCarCheckEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}
//还车
- (void)onCarReturnCheckList:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
//    用户信息做tag
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary  * dict = [BiCarBorrowParser parserCarReturnCheckListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}

-(void)onCarReturnCheck:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSDictionary  * dict = [BiCarBorrowParser parserCarReturnCheckEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:dict userInfo:nil];
    }
}
@end
